home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 26 / Mac Magazin and MacEasy Magazine CD - Issue 26.iso / Wissenschaft & Technik / Mercutio_v1.3f / Sample Codeƒ / MercutioAPI.c next >
Text File  |  1996-09-03  |  11KB  |  382 lines

  1. /***********************************************************************************
  2. **
  3. **       Developer's Programming Interface for Mercutio Menu Definition Function
  4. **               © 1992-1996 Ramon M. Felciano, All Rights Reserved
  5. **                         Latest C port -- Monday, April 22, 1996 
  6. **
  7. ************************************************************************************/
  8.  
  9. /*
  10. **    09Aug94 : Tom Emerson
  11. **    Modified by Tom Emerson (tree@bedford.symantec.com) to work correctly with
  12. **    the universal headers, and hence when calling from PowerPC native code.
  13. **    This has been conditionalized so that it will compile with and without the
  14. **  universal interfaces.
  15. **    
  16. */
  17.  
  18. /*
  19. **  03Sep96 : Bryan Pietrzak (U S WEST Marketing Resources Group)
  20. **  Support STRICT_MENUS:
  21. **        Added PrivateMenuInfo because MenuInfo does not exist with STRICT_MENUS
  22. **        Added GetMenuProc because can’t get at the menu proc by dereferencing with STRICT_MENUS
  23. **        Changed MenuHandle to MenuRef
  24. **
  25. **  11Jul96 : Uwe Hees
  26. **    Added Events.h to compile seperately.
  27. **  Removed obsolete defintion of MDEFProc.
  28. **  Changed interface to MDEF_SetCallbackProc for use with UPPs.
  29. **
  30. **  22Apr96 : RMF Updated to 1.3
  31. **    19Dec94 : RMF
  32. **    Updated to full Mercutio 1.2 spec by Ramon Felciano.
  33. **    
  34. **    27Dec94 : RMF
  35. **    MDEF_CalcItemSize now correctly returns a result.
  36. **    MDEF_StripCustomData declares Point at top of function (bug?).
  37. */
  38.  
  39. #include "MercutioAPI.h"
  40.  
  41. #ifndef __EVENTS__
  42. #include <Events.h>
  43. #endif
  44.  
  45. // 960903•ZAK in case Apple defines this in the future, define it conditionally
  46. #ifndef GetMenuProc
  47. #define GetMenuProc(menu)    (*((Handle *) ((*((Ptr *) (menu))) + 0x06)))
  48. #endif
  49.  
  50. #define     _Point2Long(pt)        (* (long *) &pt)        // these would have pbs with register vars
  51. #define     _Long2Point(long)    (* (Point *) &long)
  52. #define        _TopLeft(aRect)        (* (Point *) &(aRect).top)
  53.  
  54. /***********************************************************************************
  55. **
  56. **   MDEF_GetVersion returns the MDEF version in long form. This can be typecast
  57. **     to a normal version record if needed.
  58. **
  59. ************************************************************************************/
  60. pascal    long    MDEF_GetVersion (MenuRef menu)
  61. {
  62.     SignedByte state;
  63.     Handle    proc;
  64.     Rect    dummyRect;
  65.     short    dummyInt;
  66.     Point     pt;
  67.     MenuDefUPP    menuProcUPP;
  68.     
  69.     proc = GetMenuProc(menu);  // 960903•ZAK
  70.     state = HGetState(proc);
  71.     HLock(proc);
  72.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  73.  
  74.     SetPt(&pt,0,0);
  75.  
  76.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  77.     CallMenuDefProc(menuProcUPP, getVersionMsg, menu, &dummyRect, pt, &dummyInt);    
  78.     DisposeRoutineDescriptor(menuProcUPP);
  79.     
  80.     HSetState(proc, state);
  81.     
  82.     /* the result, a long, is returned in dummyRect.topLeft */
  83.     return _Point2Long(_TopLeft(dummyRect));
  84. }
  85.  
  86. /***********************************************************************************
  87. **
  88. **   MDEF_GetCopyright returns a stringHandle to the copyright message for the MDEF.
  89. **
  90. **   IMPORTANT: THE CALLER IS RESPONSIBLE FOR DISPOSING OF THIS HANDLE WHEN DONE
  91. **              WITH IT.
  92. **
  93. ************************************************************************************/
  94. pascal    StringHandle    MDEF_GetCopyright (MenuRef menu)
  95. {
  96.     SignedByte state;
  97.     Handle    proc;
  98.     Rect    dummyRect;
  99.     short    dummyInt;
  100.     Point     pt;
  101.     MenuDefUPP    menuProcUPP;
  102.     Point    topleft;
  103.     long    pointAsLong;
  104.     
  105.     proc = GetMenuProc(menu);  // 960903•ZAK
  106.     state = HGetState(proc);
  107.     HLock(proc);
  108.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  109.  
  110.     SetPt(&pt,0,0);
  111.  
  112.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  113.     CallMenuDefProc(menuProcUPP, getCopyrightMsg, menu, &dummyRect, pt, &dummyInt);    
  114.     DisposeRoutineDescriptor(menuProcUPP);
  115.  
  116.     HSetState(proc, state);
  117.     
  118.     /* the result, a stringHandle, is returned in dummyRect.topLeft */
  119.     topleft = _TopLeft(dummyRect);
  120.     pointAsLong = _Point2Long(topleft);
  121.     return (StringHandle)(pointAsLong);
  122. }
  123.  
  124. /***********************************************************************************
  125. **
  126. **   IsCustomMenu returns true if hMenu is controlled by a custom MDEF. This relies on my}
  127. **   convention of returning the customDefProcSig constant in the rect parameter: this obtuse}
  128. **   convention should be unique enough that only my custom MDEFs behave this way.}
  129. **
  130. ************************************************************************************/
  131. pascal    Boolean MDEF_IsCustomMenu (MenuRef menu)
  132. {
  133.     SignedByte state;
  134.     Handle    proc;
  135.     Rect    dummyRect;
  136.     short    dummyInt;
  137.     Point     pt;
  138.     MenuDefUPP    menuProcUPP;
  139.     
  140.     proc = GetMenuProc(menu);  // 960903•ZAK
  141.     state = HGetState(proc);
  142.     HLock(proc);
  143.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  144.  
  145.     SetPt(&pt,0,0);
  146.  
  147.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  148.     CallMenuDefProc(menuProcUPP, areYouCustomMsg, menu, &dummyRect, pt, &dummyInt);    
  149.     DisposeRoutineDescriptor(menuProcUPP);
  150.  
  151.     HSetState(proc, state);
  152.     
  153.     /* the result, a long, is returned in dummyRect.topLeft */
  154.     return (_Point2Long(_TopLeft(dummyRect)) == (long) (customDefProcSig));
  155. }
  156.  
  157.  
  158. /***********************************************************************************
  159. **
  160. **   MDEF_MenuKey is a replacement for the standard toolbox call MenuKey for use with the}
  161. **   Mercutio. Given the keypress message and modifiers parameters from a standard event, it }
  162. **   checks to see if the keypress is a key-equivalent for a particular menuitem. If you are currently}
  163. **   using custom menus (i.e. menus using Mercutio), pass the handle to one of these menus in}
  164. **   hMenu. If you are not using custom menus, pass in NIL or another menu, and MDEF_MenuKey will use the}
  165. **   standard MenuKey function to interpret the keypress.}
  166. **
  167. **   As with MenuKey, MDEF_MenuKey returns the menu ID in high word of the result, and the menu}
  168. **   item in the low word.}
  169. **
  170. ************************************************************************************/
  171.  
  172. pascal    long MDEF_MenuKey (long theMessage, short theModifiers, MenuRef menu)
  173. {
  174.     
  175.     if ((menu == NULL) || (!MDEF_IsCustomMenu(menu))) {
  176.         return(MenuKey((char)(theMessage & charCodeMask)));
  177.     } else {
  178.         Handle proc = GetMenuProc(menu);  // 960903•ZAK
  179.         char state = HGetState(proc);
  180.         Rect dummyRect;
  181.         Point pt = _Long2Point(theMessage);
  182.         MenuDefUPP    menuProcUPP;
  183.         
  184.         HLock(proc);
  185.         dummyRect.top = dummyRect.left = 0;
  186.         
  187.         menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  188.         CallMenuDefProc(menuProcUPP, mMenuKeyMsg, menu, &dummyRect, pt, &theModifiers);    
  189.         DisposeRoutineDescriptor(menuProcUPP);
  190.  
  191.         HSetState(proc, state);
  192.         return( _Point2Long(_TopLeft(dummyRect)));
  193.     }
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. pascal void    MDEF_SetCallbackProc(MenuRef hMenu, MercutioCallbackUPP mercutioCallback)
  211. //pascal void MDEF_SetCallbackProc (MenuRef hMenu, ProcPtr mercutioCallback)
  212. {
  213.     Rect    dummyRect;
  214.     short    dummyInt;
  215.     Point     pt;
  216.     MenuDefUPP    menuProcUPP;
  217.  
  218.     Handle proc = GetMenuProc(hMenu);  // 960903•ZAK
  219.     char state = HGetState(proc);
  220.     HLock(proc);
  221.     
  222.     pt.h = (short) (0x0000FFFF & (long) mercutioCallback);
  223.     pt.v = (short) ((long) mercutioCallback >> 16);
  224.     
  225.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  226.     CallMenuDefProc(menuProcUPP, setCallbackMsg, hMenu, &dummyRect, pt, &dummyInt);    
  227.     DisposeRoutineDescriptor(menuProcUPP);
  228.  
  229.     HSetState(proc, state);
  230. }
  231.  
  232.  
  233.  
  234.  
  235. pascal void MDEF_SetMenuPrefs (MenuRef menu, MenuPrefsRec *thePrefs)
  236. {
  237.     Rect    dummyRect;
  238.     short    dummyInt;
  239.     Point     pt;
  240.     MenuDefUPP    menuProcUPP;
  241.  
  242.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  243.     char state = HGetState(proc);
  244.     HLock(proc);
  245.  
  246.     pt.h = (short) (0x0000FFFF & (long) thePrefs);
  247.     pt.v = (short) ((long) thePrefs >> 16);
  248.  
  249.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  250.     CallMenuDefProc(menuProcUPP, setPrefsMsg, menu, &dummyRect, pt, &dummyInt);    
  251.     DisposeRoutineDescriptor(menuProcUPP);
  252.  
  253.     HSetState(proc, state);
  254.     CalcMenuSize(menu);
  255. }
  256.  
  257.  
  258.  
  259. pascal void MDEF_SetKeyGraphicsPreference (MenuRef menu, Boolean preferGraphics)
  260. {
  261.     Rect    dummyRect;
  262.     short    dummyInt;
  263.     Point     pt;
  264.     MenuDefUPP    menuProcUPP;
  265.  
  266.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  267.     char state = HGetState(proc);
  268.     HLock(proc);
  269.  
  270.     pt.h = (short) preferGraphics;
  271.     pt.v = 0;
  272.  
  273.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  274.     CallMenuDefProc(menuProcUPP, setKeyGraphicsMsg, menu, &dummyRect, pt, &dummyInt);    
  275.     DisposeRoutineDescriptor(menuProcUPP);
  276.  
  277.     HSetState(proc, state);
  278. }
  279.  
  280. pascal void MDEF_SetSmallIconID (MenuRef menu, short iconsSmallAboveID)
  281. {
  282.     Rect    dummyRect;
  283.     Point     pt;
  284.     MenuDefUPP    menuProcUPP;
  285.  
  286.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  287.     char state = HGetState(proc);
  288.     HLock(proc);
  289.  
  290.     pt.h = 0;
  291.     pt.v = 0;
  292.  
  293.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  294.     CallMenuDefProc(menuProcUPP, setSmallIconIDMsg, menu, &dummyRect, pt, &iconsSmallAboveID);    
  295.     DisposeRoutineDescriptor(menuProcUPP);
  296.  
  297.     HSetState(proc, state);
  298. }
  299.  
  300.  
  301.  
  302.  
  303. pascal void MDEF_StripCustomData (MenuRef menu)
  304. {
  305.     Point     pt = {0,0};
  306.     Rect    dummyRect;
  307.     short    dummyInt;
  308.     MenuDefUPP    menuProcUPP;
  309.  
  310.     Handle proc;
  311.     char state;
  312.     
  313.     proc = GetMenuProc(menu);  // 960903•ZAK
  314.     state = HGetState(proc);
  315.     HLock(proc);
  316.  
  317.  
  318.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  319.     CallMenuDefProc(menuProcUPP, stripCustomDataMsg, menu, &dummyRect, pt, &dummyInt);    
  320.     DisposeRoutineDescriptor(menuProcUPP);
  321.  
  322.     HSetState(proc, state);
  323. }
  324.  
  325.  
  326.  
  327.  
  328. pascal void MDEF_DrawItem (MenuRef menu, short item, Rect destRect)
  329. {
  330.     Point     pt = {0,0};
  331.     MenuDefUPP    menuProcUPP;
  332.  
  333.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  334.     char state = HGetState(proc);
  335.     HLock(proc);
  336.  
  337.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  338.     CallMenuDefProc(menuProcUPP, mDrawItemMsg, menu, &destRect, pt, &item);    
  339.     DisposeRoutineDescriptor(menuProcUPP);
  340.  
  341.     HSetState(proc, state);
  342. }
  343.  
  344.  
  345. pascal void MDEF_DrawItemState (MenuRef menu, short item, Rect destRect, Boolean isHilited, Boolean isEnabled)
  346. {
  347.     Point    pt;
  348.     MenuDefUPP    menuProcUPP;
  349.  
  350.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  351.     char state = HGetState(proc);
  352.     HLock(proc);
  353.  
  354.     pt.h = (short) isHilited;
  355.     pt.v = (short) isEnabled;
  356.  
  357.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  358.     CallMenuDefProc(menuProcUPP, mDrawItemStateMsg, menu, &destRect, pt, &item);    
  359.     DisposeRoutineDescriptor(menuProcUPP);
  360.  
  361.     HSetState(proc, state);
  362. }
  363.  
  364.  
  365. pascal void MDEF_CalcItemSize (MenuRef menu, short item, Rect *destRect)
  366. {
  367.     Point     pt = {0,0};
  368.     MenuDefUPP    menuProcUPP;
  369.  
  370.     Handle proc = GetMenuProc(menu);  // 960903•ZAK
  371.     char state = HGetState(proc);
  372.     HLock(proc);
  373.  
  374.     menuProcUPP = (MenuDefUPP)NewRoutineDescriptor((ProcPtr)*proc, uppMenuDefProcInfo, kM68kISA);
  375.     CallMenuDefProc(menuProcUPP, mCalcItemMsg, menu, destRect, pt, &item);    
  376.     DisposeRoutineDescriptor(menuProcUPP);
  377.  
  378.     HSetState(proc, state);
  379. }
  380.  
  381.  
  382.